package com.tchepannou.limbe.gae.converter;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.Text;
import com.tchepannou.limbe.annotation.Embed;
import com.tchepannou.util.converter.Converter;
import com.tchepannou.util.converter.ConverterFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * User: herve
 * Date: 14-01-15 8:11 PM
 */
public class GaeConverterFactory implements ConverterFactory
{
    //-- Attributes
    private Map<Class, Converter> _map = new HashMap<Class, Converter>();


    //-- Constructor
    public GaeConverterFactory ()
    {
        _map.put(Text.class, new TextConverter());
        _map.put(Email.class, new EmailConverter());
        _map.put(Link.class, new LinkConverter());
    }

    //-- ConverterFactory overrides
    @Override
    public Converter getConverter(Class type)
    {
        if (type.getAnnotation(Embed.class) != null)
        {
            Converter cv = _map.get(type);
            if (cv == null)
            {
                cv = new EmbedConverter(type);
                _map.put(type, cv);
            }
            return cv;
        }
        else
        {
            return _map.get(type);
        }
    }
}
